import Vue from 'vue'
import Router from 'vue-router'
import VuexStore from '@/store'
import login from '@/system/login'
import home from '@/system/pages/home'
import message from '@/system/pages/message'
import { Message } from 'element-ui'
import Layout from '@/layout'
import CustomRouterView from '@/layout/components/CustomRouterView'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth'
Vue.use(Router)
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return routerPush.call(this, location).catch(error => error)
}

const createRouter = () =>
	new Router({
		scrollBehavior: () => ({ y: 0 }),
		routes: [
			{
				path: '/login',
				name: 'login',
				component: login,
				meta: {
					title: '登陆',
				},
			},
			{
				path: '/redirect',
				component: Layout,
				hidden: true,
				children: [
					{
						path: '/redirect/:path(.*)',
						component: () => import('@/views/redirect/index'),
					},
				],
			},
			{
				path: '/asset',
				component: Layout,
				name: 'asset',
				meta: {
					title: '资产管理',
				},
				children: [
					{
						path: '/assetQuery',
						component: CustomRouterView,
						name: 'assetQuery',
						meta: { title: '资产查询', hidden: true },
						redirect: '/asset/query',
						children: [
							{
								path: '/assetInfo/:id',
								component: () => import('@/views/asset/assetQueryInfo'),
								name: 'assetInfo',
								props: true,
								meta: { title: '资产详情', hidden: true },
							},
						],
					},
					{
						path: '/assetAdd',
						component: CustomRouterView,
						name: 'assetAdd',
						meta: { title: '资产登记', hidden: true },
						redirect: '/asset/assetAddList',
						children: [
							{
								path: '/creatAsset/:wfId',
								component: () => import('@/views/asset/assetAdd'),
								name: 'creatAsset',
								props: true,
								meta: { title: '创建资产登记单', hidden: true },
							},
							{
								path: '/showAssetInfo/:id',
								component: () => import('@/views/asset/assetAddInfo'),
								name: 'showAssetInfo',
								props: true,
								meta: { title: '资产登记详情', hidden: true },
							},
						],
					},
					{
						path: '/assetPutaway',
						component: CustomRouterView,
						name: 'assetPutaway',
						meta: { title: '上架管理', hidden: true },
						redirect: '/asset/assetPutawayList',
						children: [
							{
								path: '/assetPutawayAdd/:wfId',
								component: () => import('@/views/asset/assetPutawayAdd'),
								name: 'assetPutawayAdd',
								props: true,
								meta: { title: '创建上架申请单', hidden: true },
							},
							{
								path: '/assetPutawayInfo/:id',
								component: () => import('@/views/asset/assetPutawayInfo'),
								name: 'assetPutawayInfo',
								props: true,
								meta: { title: '上架申请单详情', hidden: true },
							},
						],
					},
					{
						path: '/assetOut',
						component: CustomRouterView,
						name: 'assetOut',
						meta: { title: '下架管理', hidden: true },
						redirect: '/asset/outList',
						children: [
							{
								path: '/assetOutAdd/:wfId',
								component: () => import('@/views/asset/assetOutAdd'),
								name: 'assetOutAdd',
								props: true,
								meta: { title: '创建下架申请单', hidden: true },
							},
							{
								path: '/assetOutInfo/:id',
								component: () => import('@/views/asset/assetOutInfo'),
								name: 'assetOutInfo',
								props: true,
								meta: { title: '下架申请单详情', hidden: true },
							},
						],
					},
					{
						path: '/assetChange',
						component: CustomRouterView,
						name: 'assetChange',
						meta: { title: '变更管理', hidden: true },
						redirect: '/asset/assetChangeList',
						children: [
							{
								path: '/assetChangeAdd/:wfId',
								component: () => import('@/views/asset/assetChangeAdd'),
								name: 'assetChangeAdd',
								props: true,
								meta: { title: '创建变更申请单', hidden: true },
							},
							{
								path: '/assetChangeInfo/:id',
								component: () => import('@/views/asset/assetChangeInfo'),
								name: 'assetChangeInfo',
								props: true,
								meta: { title: '变更申请单详情', hidden: true },
							},
						],
					},
					{
						path: '/assetScrap',
						component: CustomRouterView,
						name: 'assetScrap',
						meta: { title: '报废管理', hidden: true },
						redirect: '/asset/assetScrapList',
						children: [
							{
								path: '/assetScrapAdd/:wfId',
								component: () => import('@/views/asset/assetScrapAdd'),
								name: 'assetScrapAdd',
								props: true,
								meta: { title: '创建报废申请单', hidden: true },
							},
							{
								path: '/assetScrapInfo/:id',
								component: () => import('@/views/asset/assetScrapInfo'),
								name: 'assetScrapInfo',
								props: true,
								meta: { title: '报废申请单详情', hidden: true },
							},
						],
					},
				],
			},
			{
				path: '/ipManage',
				component: Layout,
				name: 'ipManage',
				meta: {
					title: 'IP地址管理',
				},
				children: [
					{
						path: '/ipManage',
						component: CustomRouterView,
						name: 'ipManage',
						redirect: '/ipManage/ipManage',
						meta: { title: 'IP地址管理', hidden: true },
						redirect: '/ipManage/ipManage',
						children: [
							{
								path: 'detail/:ip',
								component: () => import('@/views/ipManage/ipDetail'),
								name: 'ipDetail',
								props: true,
								meta: { title: 'IP地址详情', hidden: true },
							},
						],
					},
					{
						path: '/ipAllocation',
						component: CustomRouterView,
						name: 'ipAllocation',
						meta: { title: 'IP地址分配', hidden: true },
						redirect: '/ipManage/ipAllocation',
						children: [
							{
								path: 'allocation/:id',
								component: () => import('@/views/ipManage/allocationPage'),
								name: 'allocation',
								props: true,
								meta: { title: 'IP地址申请单详情', hidden: true },
							},
						],
					},
				],
			},
			{
				path: '/rabotManage',
				component: Layout,
				name: 'rabotManage',
				meta: {
					title: '智能识别管理',
				},
				children: [
					{
						path: '/rabotManage',
						component: CustomRouterView,
						name: 'rabotManage',
						redirect: '/rabotManage/cabinet',
						meta: { title: '库房列表', hidden: true },
						redirect: '/rabotManage/cabinet',
						children: [
							{
								path: 'cabinet',
								component: () => import('@/views/rabotManage/cabinet'),
								name: 'cabinet',
								props: true,
								meta: { title: '机柜监视', hidden: true },
							},
							{
								path: 'video',
								component: () => import('@/views/rabotManage/video'),
								name: 'video',
								props: true,
								meta: { title: '机柜视频', hidden: true },
							},
						],
					}
				],
			},
			{
				path: '/',
				component: Layout,
				redirect: '/asset/statistic',
				children: [
					{
						path: '/home',
						component: home,
						name: 'home',
						meta: { title: '首页', icon: 'dashboard', affix: true },
					},
					// {
					// 	path: '/test',
					// 	name: 'test',
					// 	component: () => import('@/business/pages/workOfDay'),
					// 	meta: {
					// 		title: '测试页',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/definition',
					// 	name: 'definition',
					// 	component: () => import('@/system/pages/definition'),
					// 	meta: {
					// 		title: '报表定义',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/message',
					// 	name: 'message',
					// 	component: message,
					// 	meta: {
					// 		title: '消息中心',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/listStructure',
					// 	name: 'listStructure',
					// 	component: () => import('@/system/pages/listStructure'),
					// 	meta: {
					// 		title: '表结构',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/organization/index',
					// 	name: 'organization',
					// 	component: () => import('@/system/pages/organization/index'),
					// 	meta: {
					// 		title: '组织权限',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/menuManage',
					// 	name: 'menuManege',
					// 	component: () => import('@/system/pages/menuManage'),
					// 	meta: {
					// 		title: '菜单管理',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/log',
					// 	name: 'log',
					// 	component: () => import('@/system/pages/log'),
					// 	meta: {
					// 		title: '日志',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/customPage',
					// 	name: 'customPage',
					// 	component: () => import('@/system/pages/customPage/index'),
					// 	meta: {
					// 		title: '自定义页面',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/remindManage',
					// 	name: 'remindManage',
					// 	component: () => import('@/system/pages/remindManage'),
					// 	meta: {
					// 		title: '预警管理',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/cellEdit',
					// 	name: 'cellEdit',
					// 	component: () => import('@/system/pages/cellEdit'),
					// 	meta: {
					// 		title: '表单管理',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/bussinessEdit',
					// 	name: 'bussinessEdit',
					// 	component: () => import('@/system/pages/bussinessEdit'),
					// 	meta: {
					// 		title: '业务模型',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/flowManage',
					// 	name: 'flowManage',
					// 	component: () => import('@/system/pages/flowManage'),
					// 	meta: {
					// 		title: '流程管理',
					// 		keepAlive: true,
					// 	},
					// },
					{
						path: '/messager',
						name: 'messager',
						component: () => import('@/system/pages/messager'),
						meta: {
							title: '信使管理',
							keepAlive: true,
						},
					},
					// {
					// 	path: '/dataManage',
					// 	name: 'dataManage',
					// 	component: () => import('@/business/pages/dataManage'),
					// 	meta: {
					// 		title: '文档管理',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/materialCoding',
					// 	name: 'materialCoding',
					// 	component: () => import('@/business/pages/materialCoding'),
					// 	meta: {
					// 		title: '物资编码维护',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/dictionary',
					// 	name: 'dictionary',
					// 	component: () => import('@/system/pages/dictionary'),
					// 	meta: {
					// 		title: '数据字典',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/fundAccount',
					// 	name: 'fundAccount',
					// 	component: () => import('@/business/pages/fundAccount'),
					// 	meta: {
					// 		title: '资金科目管理',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/partsOrder',
					// 	name: 'partsOrder',
					// 	component: () => import('@/business/pages/partsOrder'),
					// 	meta: {
					// 		title: '新增采购订单',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/partsIn',
					// 	name: 'partsIn',
					// 	component: () => import('@/business/pages/partsIn'),
					// 	meta: {
					// 		title: '新增采购入库',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/loggingDesk',
					// 	name: 'loggingDesk',
					// 	component: () =>
					// 		import('@/business/pages/dutyManagement/loggingDesk'),
					// 	meta: {
					// 		title: '值班工作台',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/scheduling',
					// 	name: 'scheduling',
					// 	component: () =>
					// 		import('@/business/pages/dutyManagement/scheduling'),
					// 	meta: {
					// 		title: '排班管理',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/examinationsManage',
					// 	name: 'examinationsManage',
					// 	component: () =>
					// 		import('@/business/pages/examination/examinationsManage'),
					// 	meta: {
					// 		title: '在线考试',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/examinationsSignUp',
					// 	name: 'examinationsSignUp',
					// 	component: () =>
					// 		import('@/business/pages/examination/examinationsSignUp'),
					// 	meta: {
					// 		title: '考试报名',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/workOfDay',
					// 	name: 'workOfDay',
					// 	component: () => import('@/business/pages/workOfDay'),
					// 	meta: {
					// 		title: '日管控',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '/dataOrigin',
					// 	name: 'dataOrigin',
					// 	component: () => import('@/system/pages/dataOrigin'),
					// 	meta: {
					// 		title: '数据源',
					// 		keepAlive: true,
					// 	},
					// },
					// {
					// 	path: '*',
					// 	redirect: '/home',
					// },
					// {
					// 	path: '/zctj',
					// 	path: 'zctj',
					// 	component: () => import('@/views/asset/statistic/statisticView'),
					// 	meta: {
					// 		title: '资产统计',
					// 	},
					// },
				],
			},
		],
	})
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
	const newRouter = createRouter()
	router.matcher = newRouter.matcher // reset router
}
export default router
const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist
router.beforeEach(async (to, from, next) => {
	// start progress bar
	// NProgress.start()

	// set page title
	// document.title = getPageTitle(to.meta.title)

	// determine whether the user has logged in
	const hasToken = getToken()

	if (hasToken) {
		if (to.path === '/login') {
			// if is logged in, redirect to the home page
			next({ path: '/' })
			// NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
		} else {
			// determine whether the user has obtained his permission roles through getInfo
			const hasLoadRouters = VuexStore.getters.permission_routes && VuexStore.getters.permission_routes.length > 0
			if (hasLoadRouters) {
				next()
			} else {
				try {
					// get user info
					// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
					const routerList = await VuexStore.dispatch('permission/loadPermission')

					// generate accessible routes map based on roles
					const accessRoutes = await VuexStore.dispatch('permission/generateRoutes', routerList)
					if (accessRoutes && accessRoutes.length > 0) {
						accessRoutes.forEach(item => {
							router.addRoute(item)
						})
					}
					// dynamically add accessible routes
					// router.addRoutes(accessRoutes)

					// hack method to ensure that addRoutes is complete
					// set the replace: true, so the navigation will not leave a history record
					next({ ...to, replace: true })
				} catch (error) {
					// remove token and go to login page to re-login
					await VuexStore.dispatch('FedLogOut')
					console.log(error)
					// Message.error({
					// 	message: error.data.data || '出现错误，请稍后再试',
					// })
					// next(`/login?redirect=${to.path}`)
					next(`/login`)
					// NProgress.done()
				}
			}
		}
	} else {
		/* has no token*/

		if (whiteList.indexOf(to.path) !== -1) {
			// in the free login whitelist, go directly
			next()
		} else {
			// other pages that do not have permission to access are redirected to the login page.
			// next(`/login?redirect=${to.path}`)
			next(`/login`)
			// NProgress.done()
		}
	}
})

// router.afterEach(() => {
// 	// finish progress bar
// 	// NProgress.done()
// })
